/*
 * Logisim-evolution - digital logic design tool and simulator
 * Copyright by the Logisim-evolution developers
 *
 * https://github.com/logisim-evolution/
 *
 * This is free software released under GNU GPLv3 license
 */

package com.cburch.logisim.std.wiring;

import com.cburch.logisim.circuit.RadixOption;
import com.cburch.logisim.comp.EndData;
import com.cburch.logisim.data.Attribute;
import com.cburch.logisim.data.AttributeOption;
import com.cburch.logisim.data.BitWidth;
import com.cburch.logisim.instance.StdAttr;

import java.util.ArrayList;
import java.util.List;

class PinAttributes extends ProbeAttributes {
  /* I introduced the dummy attribute to be backward compatible with files generated by
   * the holycross branch of logisim (v4.0 up)
   */
  private static class DummyAttr extends Attribute<String> {
    public DummyAttr(String name) {
      super(name, null);
    }

    @Override
    public String parse(String value) {
      return value;
    }

    @Override
    public boolean isHidden() {
      return true;
    }
  }

  public static final Attribute<String> ATTR_DUMMY = new DummyAttr("type");
  public static PinAttributes instance = new PinAttributes();

  private List<Attribute<?>> myAttributes;

  BitWidth width = BitWidth.ONE;
  boolean threeState = false; // true;
  int type = EndData.INPUT_ONLY;
  Object pull = Pin.PULL_NONE;
  Long initialValue = 0L;

  public PinAttributes() {
    updateAttributes();
  }

  private void updateAttributes() {
    myAttributes = new ArrayList<Attribute<?>>();
    myAttributes.add(StdAttr.FACING);
    myAttributes.add(Pin.ATTR_TYPE);
    myAttributes.add(StdAttr.WIDTH);
    myAttributes.add(Pin.ATTR_TRISTATE);
    myAttributes.add(Pin.ATTR_PULL);
    myAttributes.add(StdAttr.LABEL);
    myAttributes.add(StdAttr.LABEL_FONT);
    myAttributes.add(RadixOption.ATTRIBUTE);
    if ((type == EndData.INPUT_ONLY) && !threeState) {
      myAttributes.add(Pin.ATTR_INITIAL);
    }
    myAttributes.add(PROBEAPPEARANCE);
    myAttributes.add(ATTR_DUMMY);
  }

  @Override
  public List<Attribute<?>> getAttributes() {
    return myAttributes;
  }

  @Override
  public boolean isToSave(Attribute<?> attr) {
    return attr.isToSave() && attr != ATTR_DUMMY;
  }

  @Override
  @SuppressWarnings("unchecked")
  public <V> V getValue(Attribute<V> attr) {
    if (attr == StdAttr.WIDTH) return (V) width;
    if (attr == Pin.ATTR_TRISTATE) return (V) Boolean.valueOf(threeState);
    if (attr == Pin.ATTR_TYPE) return (V) Boolean.valueOf(type == EndData.OUTPUT_ONLY);
    if (attr == Pin.ATTR_PULL) return (V) pull;
    if (attr == PROBEAPPEARANCE) return (V) appearance;
    if (attr == ATTR_DUMMY) return (V) "nochange";
    if (attr == Pin.ATTR_INITIAL) return (V) initialValue;
    return super.getValue(attr);
  }

  boolean isInput() {
    return type != EndData.OUTPUT_ONLY;
  }

  boolean isOutput() {
    return type != EndData.INPUT_ONLY;
  }

  @SuppressWarnings("unchecked")
  @Override
  public <V> void setValue(Attribute<V> attr, V value) {
    if (attr == ATTR_DUMMY) {
      if (value.equals("output")) {
        if (type != EndData.OUTPUT_ONLY) {
          type = EndData.OUTPUT_ONLY;
          fireAttributeValueChanged(
              (Attribute<V>) Pin.ATTR_TYPE, (V) Boolean.valueOf(type == EndData.OUTPUT_ONLY), null);
          return;
        }
      }
    } else if (attr == StdAttr.WIDTH) {
      BitWidth NewWidth = (BitWidth) value;
      if (width == NewWidth) return;
      width = (BitWidth) value;
      if (width.getWidth() > 8 && appearance == ProbeAttributes.APPEAR_EVOLUTION_NEW)
        super.setValue(RadixOption.ATTRIBUTE, RadixOption.RADIX_16);
    } else if (attr == Pin.ATTR_TRISTATE) {
      boolean NewThree = (Boolean) value;
      if (threeState == NewThree) return;
      threeState = NewThree;
      updateAttributes();
      fireAttributeListChanged();
    } else if (attr == Pin.ATTR_TYPE) {
      int Newtype = (Boolean) value ? EndData.OUTPUT_ONLY : EndData.INPUT_ONLY;
      if (type == Newtype) return;
      type = Newtype;
      updateAttributes();
      fireAttributeListChanged();
    } else if (attr == Pin.ATTR_PULL) {
      if (pull.equals(value)) return;
      pull = value;
    } else if (attr == PROBEAPPEARANCE) {
      final var newAppearance = (AttributeOption) value;
      if (appearance.equals(newAppearance)) return;
      appearance = newAppearance;
    } else if (attr == RadixOption.ATTRIBUTE) {
      if (width.getWidth() == 1) {
        super.setValue(RadixOption.ATTRIBUTE, RadixOption.RADIX_2);
      } else super.setValue(attr, value);
      return;
    } else if (attr == Pin.ATTR_INITIAL) {
      final var newInitial = (Long) value;
      if (newInitial == initialValue) return;
      initialValue = newInitial;
    } else {
      super.setValue(attr, value);
      return;
    }
    fireAttributeValueChanged(attr, value, null);
  }
}
